﻿using System;
using System.ComponentModel.Composition;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Red.Controls.Diagrams.ViewModels;
using Red.Controls.Shell;
using Red.Core.Extensions;
using Red.EngineData.View;

namespace Red.Toolkit.AnimationGraphEditor
{
    [Export( typeof( IEditorInfo ) )]
    public class EditorInfo : IEditorInfo
    {
        public string Name
        {
            get { return "Animation Graph Editor"; }
        }

        public Type WindowClass
        {
            get { return typeof( AnimationGraphEditor ); }
        }

        public Type SettingsClass
        {
            get { return typeof( UserSettings ); }
        }
    }

    /// <summary>
    /// Interaction logic for AnimationGraphEditor.xaml
    /// </summary>
    public partial class AnimationGraphEditor : RedEditorWindow
    {
        #region Data

        private readonly EditorInstanceData _instanceData;
        private readonly EditorSharedData _sharedData;

        #endregion Data

        #region C-tor

        public AnimationGraphEditor( IEditorHost host, EditorInstanceData instanceData, EditorSharedData sharedData )
            : base( host, instanceData, sharedData )
        {
            _instanceData = instanceData;
            _sharedData = sharedData;
            InitializeComponent();
            LoadDeclarators();
            BackendInitialized += AnimationGraphEditor_BackendInitialized;
        }

        #endregion C-tor

        #region Private methods

        private void LoadDeclarators()
        {
            var engineRootPath = Path.Combine( SharedData.DepotInfo.RootPath, SharedData.EnginePath );
            var filePath = Path.Combine( engineRootPath, @"nodedeclarators\animGraph.dcl" );
            _nodeControl.DeclaratorsPath = filePath;
        }

        #endregion Private methods

        #region Event handlers

        private async void AnimationGraphEditor_BackendInitialized( object sender, EventArgs e )
        {
            await _nodeControl.LoadAsync( _instanceData.RootEditorItem.Data );
            _variablesAndEventsPage.Init( _instanceData.RootEditorItem.Data );
            _animBrowserPage.Init( _instanceData.RootEditorItem.Data );

            // For William: use this code if you are ready to test your preview.
            //_preview.Init( _instanceData.RootEditorObject, _sharedData.EngineMonitor );
        }

        #endregion Event handlers


        protected override IntPtr? GetPreviewHandle()
        {
            return _previewPage._preview.Handle;
        }

        private void NodeControl_SelectionChanged( object sender, SelectionChangedEventArgs e )
        {
            if ( e.AddedItems.Count == 1 )
            {
                // selected one node
                var selectedNodeViewModel = e.AddedItems[ 0 ] as NodeViewModel;
                var iObject = selectedNodeViewModel.Object;
                MainDataCategory.DataContext = iObject.GetDataItem();

                // TODO: this probably will not be needed after we preserve expanded state despite changing DataContext
                MainDataCategory.IsExpanded = true;
            }            
        }
    }
}
